package chapter06

/**
 * 伴生对象
 * 概念和应用
 */
object Test11_Object {
  def main(args: Array[String]): Unit = {
    //    val alice = new Student11("alice", 18)
    //    alice.printInfo()

    val student1 = Student11.newStudent("alice", 18)
    student1.printInfo()

    val student2 = Student11.apply("bob", 19)
    student2.printInfo()

    //上面简写
    val student3 = Student11("bob", 22)
    student3.printInfo()
  }
}


// 写在构造器前面private 私有化构造器
class Student11 private(val name: String, val age: Int) {
  def printInfo(): Unit = {
    println(s"student: name =${name}, age =$age, school =${Student11.school}")
  }
}


/**
 * 伴生对象里面的加的属性 和方法 属于静态的
 *
 * 在当前的半生类对象里面可以直接访问当前伴生类的私有成员
 */
object Student11 {
  var school: String = "atguigu"

  def newStudent(name: String, age: Int): Student11 = new Student11(name, age)

  def apply(name: String, age: Int): Student11 = new Student11(name, age)

}
